using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for DatabaseTools
/// </summary>
public class DatabaseTools
{
    SqlConnection conn = new SqlConnection();
    string database;
    string sever;
    string username;
    string pass;

	public DatabaseTools()
	{
		//
		// TODO: Add constructor logic here
		//
        string connstring;
        //sever = System.Configuration.ConfigurationManager.AppSettings.Get("sever");
        //database = System.Configuration.ConfigurationManager.AppSettings.Get("database");
        //username = System.Configuration.ConfigurationManager.AppSettings.Get("username");
        //pass = System.Configuration.ConfigurationManager.AppSettings.Get("pass");
        //connstring = "server=" + sever + ";UID=" + username + ";PWD=" + pass + ";database=" + database + "";
        connstring=ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        conn.ConnectionString = connstring;
        try
        {
            conn.Open();
            if (conn.State != System.Data.ConnectionState.Open)
            { }

        }
        catch { }

	}
    public string GetValueSQL(string SQL)
    {

        SqlDataAdapter da;
        DataSet ds = new DataSet();
        da = new SqlDataAdapter(SQL, conn);
        try
        {
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count == 0) return "";
            return ds.Tables[0].Rows[0][0].ToString();
        }
        catch { return ""; }

    }


   
    public Boolean SQLExecuteNonQuery(string SQL)
    {
        SqlCommand cm = new SqlCommand(SQL, conn);
        try
        {
            cm.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }

    }

    
    public DataTable SQLGetTable(string SQL)
    {
        SqlDataAdapter da;
        DataSet ds = new DataSet();
        da = new SqlDataAdapter(SQL, conn);
        try
        {
            da.Fill(ds);
            if (ds.Tables.Count == 0) return null;
            return ds.Tables[0];
        }
        catch { return null; }
    }

   
    public Boolean Set_SQLConfig(string ser, string data, string user, string pass)
    {
        string connstring;
        SqlConnection conn1 = new SqlConnection();
        connstring = "server=" + ser + ";UID=" + user + ";PWD=" + pass + ";database=" + data + "";
        conn1.ConnectionString = connstring;
        try
        {
            conn1.Open();
            if (conn1.State != System.Data.ConnectionState.Open)
            {
                return false;
            }
            System.Configuration.ConfigurationManager.AppSettings.Set("sever", ser);
            System.Configuration.ConfigurationManager.AppSettings.Set("database", data);
            System.Configuration.ConfigurationManager.AppSettings.Set("username", user);
            System.Configuration.ConfigurationManager.AppSettings.Set("pass", pass);
            return true;
        }
        catch
        {
            return false;
        }
    }
    
    public string Get_SQLConfig()
    {
        string[] Re = new string[4];
        Re[0] = System.Configuration.ConfigurationManager.AppSettings.Get("sever");
        Re[1] = System.Configuration.ConfigurationManager.AppSettings.Get("database");
        Re[2] = System.Configuration.ConfigurationManager.AppSettings.Get("username");
        Re[3] = System.Configuration.ConfigurationManager.AppSettings.Get("pass");
        return "server=" + Re[0] + ";UID=" + Re[2] + ";PWD=" + Re[3] + ";database=" + Re[1] + "";
    }
}
